package com.example.deleguesapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.delegues.model.User;
import com.example.deleguesapp.manager.DataManager;
import com.example.deleguesapp.manager.DeleguesManager;

public class AppreciationActivity extends Activity {
	private User eleve;


	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appreciation);
		this.eleve = DataManager.getInstance().getUser();
		final TextView textUser = (TextView) findViewById(R.id.textView1);
		textUser.setText(eleve.getForname()+" "+eleve.getName());
		//textview moyenne generale
		final TextView textMoyenne = (TextView) findViewById(R.id.textView2);
		float moyenneGenerale = eleve.getMoyG();
		if (moyenneGenerale != 0)				
			textMoyenne.setText(Float.toString(moyenneGenerale));
		Button boutonMoyenne = (Button) findViewById(R.id.button1);
		boutonMoyenne.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder alert = new AlertDialog.Builder(AppreciationActivity.this);

				alert.setTitle("Editer la moyenne");
				alert.setMessage("Rentrez la moyenne de l'eleve");

				// Set an EditText view to get user input 
				final EditText input = new EditText(AppreciationActivity.this);
				input.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);				
				input.setText(textMoyenne.getText());
				input.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						input.setText("");
						
					}
				});
				alert.setView(input);

				alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
				  Editable value = input.getText();
				  float newMoyenne = Float.valueOf(value.toString());
				  if (newMoyenne<0 || newMoyenne>20)
					  Toast.makeText(AppreciationActivity.this, "Moyenne non valide", Toast.LENGTH_SHORT).show();
				  else{
				  eleve.setMoyG(newMoyenne);
				  DeleguesManager.getInstance(AppreciationActivity.this).saveUser(eleve);
				  textMoyenne.setText(value);
				  }
				  }
				});

				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				  public void onClick(DialogInterface dialog, int whichButton) {
				    // Canceled.
				  }
				});

				alert.show();
			}
		});
		final TextView textAppreciation = (TextView) findViewById(R.id.textView3);
		String appreciation = eleve.getAppreciation();
		if (appreciation != null)
			textAppreciation.setText(appreciation);
		Button boutonAppreciation = (Button) findViewById(R.id.button2);
		boutonAppreciation.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				AlertDialog.Builder builder = new AlertDialog.Builder(AppreciationActivity.this);

				builder.setTitle("Editer l'appreciation");
				
				final CharSequence[] items = {"Felicitations", "Tableau d'honneur", "Encouragements", "Aucun", "Avertissement"};
				
				builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
					
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
						
						Toast.makeText(AppreciationActivity.this, items[which], Toast.LENGTH_SHORT).show();
						
					}
				});

				
				builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						ListView lw = ((AlertDialog)dialog).getListView();
						String checkedItem = (String) lw.getAdapter().getItem(lw.getCheckedItemPosition());
						eleve.setAppreciation(checkedItem);
						DeleguesManager.getInstance(AppreciationActivity.this).saveUser(eleve);
						textAppreciation.setText(checkedItem);
					}
				});
				
				builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				  public void onClick(DialogInterface dialog, int whichButton) {
				    // Canceled.
				  }
				});
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
		final TextView textRemarque = (TextView) findViewById(R.id.textView4);
		String remarques = eleve.getRemarques();
		if (remarques != null)
			textRemarque.setText(remarques);
		Button boutonRemarque = (Button) findViewById(R.id.button3);
		boutonRemarque.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder alert = new AlertDialog.Builder(AppreciationActivity.this);

				alert.setTitle("Remarques");
				alert.setMessage("Rentrez les remarques du conseil de classe");

				// Set an EditText view to get user input 
				final EditText input = new EditText(AppreciationActivity.this);
				input.setText(textRemarque.getText());
				if (input.getText().toString().equals("Remarques"))
					input.setOnClickListener(new OnClickListener() {
						
						public void onClick(View v) {
							// TODO Auto-generated method stub
							input.setText("");
						}
					});
				alert.setView(input);

				alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
				  Editable value = input.getText();
				  eleve.setRemarques(value.toString());
				  textRemarque.setText(value);
				  DeleguesManager.getInstance(AppreciationActivity.this).saveUser(eleve);
				  }
				});

				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				  public void onClick(DialogInterface dialog, int whichButton) {
				    // Canceled.
				  }
				});

				alert.show();
				
			}
		});
		
	}

}
